package com.ldx.blog.sevice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ldx.blog.common.exception.BlogException;
import com.ldx.blog.mapper.BlogInfoMapper;
import com.ldx.blog.pojo.dataobject.BlogInfo;
import com.ldx.blog.pojo.request.AddBlogRequest;
import com.ldx.blog.pojo.request.UpdateBlogRequest;
import com.ldx.blog.pojo.response.BlogInfoResponse;
import com.ldx.blog.sevice.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class BlogServiceImpl implements BlogService {
    @Resource
    private BlogInfoMapper blogInfoMapper;
    @Override
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag,0);
         List<BlogInfo> blogInfos = blogInfoMapper.selectList(queryWrapper);
//         List<BlogInfoResponse> blogInfoResponses = blogInfos.stream().map(x->{
//             BlogInfoResponse blogInfoResponse = new BlogInfoResponse();
//             BeanUtils.copyProperties(x,blogInfoResponse);
//             return blogInfoResponse;
//         }).collect(Collectors.toList());
        List<BlogInfoResponse> blogInfoResponses = new ArrayList<>();
        for(BlogInfo blogInfo:blogInfos){
            BlogInfoResponse blogInfoResponse = new BlogInfoResponse();
            BeanUtils.copyProperties(blogInfo,blogInfoResponse);
            blogInfoResponses.add(blogInfoResponse);
        }

        return blogInfoResponses;
    }

    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<BlogInfo>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag,0)
                        .eq(BlogInfo::getId,blogId);
        BlogInfo blogInfo = blogInfoMapper.selectOne(queryWrapper);

        return BlogInfoResponse.trans(blogInfo);
    }

    @Override
    public Boolean add(AddBlogRequest addBlogRequest) throws BlogException {
        try{
            Integer result =  blogInfoMapper.insert(AddBlogRequest.trans(addBlogRequest));
            if(result == 1)
                return true;
        }catch (Exception e){
            log.error("添加博客出错 e:"+e);
            throw new BlogException("添加失败");
        }
        return false;
    }

    @Override
    public Boolean updateBlog(UpdateBlogRequest updateBlogRequest) throws BlogException {
        try {
            int i = blogInfoMapper.updateById(UpdateBlogRequest.trans(updateBlogRequest));
            if(i == 1){
                return true;
            }else {
                return false;
            }
        }catch (Exception e){
            log.error("updateBlog异常 e:",e);
            throw new BlogException("内部错误请联系管理员");
        }
    }

    @Override
    public Boolean deleteBlog(Integer blogId) throws BlogException {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        try {
            int i = blogInfoMapper.updateById(blogInfo);
            if(i == 1){
                return true;
            }else {
                return false;
            }
        }catch (Exception e){
            log.error("deleteBlog异常 e:",e);
            throw new BlogException("内部错误请联系管理员");
        }
    }
}
